#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_exam1.py
# author:王雯雯
# datetime:2024-12-7
# software: PyCharm

import threading
import random


# 定义全局变量用于存储同学分数
student_scores = []


def generate_scores():
    global student_scores
    for _ in range(100):
        student_scores.append(random.randint(0, 100))


def print_scores(start, end):
    for index in range(start, end):
        print(f"同学{index + 1}的分数: {student_scores[index]}")

#多线程
if __name__ == "__main__":
    generate_scores()
    threads = []
    for i in range(5):
        start_index = i * 20
        end_index = (i + 1) * 20
        thread = threading.Thread(target=print_scores, args=(start_index, end_index))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()
import concurrent.futures
import random


# 定义全局变量用于存储同学分数
student_scores = []


def generate_scores():
    global student_scores
    for _ in range(100):
        student_scores.append(random.randint(0, 100))


def print_scores(start, end):
    for index in range(start, end):
        print(f"同学{index + 1}的分数: {student_scores[index]}")

# 用线程池 实现
if __name__ == "__main__":
    generate_scores()
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        futures = []
        for i in range(5):
            start_index = i * 20
            end_index = (i + 1) * 20
            future = executor.submit(print_scores, start_index, end_index)
            futures.append(future)

        for future in concurrent.futures.as_completed(futures):
            result = future.result()
